<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\User;
use app\admin\validate\UserValidate;

class Login extends Controller {

    public function index(){
        // phpinfo();
        $this->view->engine->layout(false);
        return $this->fetch();
    }

    public function login(){
        //接收参数
        if($this->request->isPost()){
            $username = $this->request->post('username','','trim');
            $password = $this->request->post('password','','trim');
            $validate = new UserValidate;
            $data = ['username'=>$username,'password'=>$password];
            if(!$validate->check($data)){
                $url = url('login/index');
                header("Refresh:1;url={$url}");
                return $validate->getError();
            }else{
                $url = url('index/index');
                $user = new User;
                if($user_info = $user->checkUser($username,$password)){
                    session('uid',$user_info['id']);
                    session('nickname',$user_info['nickname']);
                    header("Refresh:1;url={$url}");
                    return "登陆成功";
                }else{
                    $url = url('login/index');
                    header("Refresh:1;url={$url}");
                    return "用户名或密码错误";
                }
            }
        }
    }

    public function logout() {
        session(null);
        $url = url('login/index');
        header("Location:{$url}");
    }



}